const COS = require("cos-nodejs-sdk-v5");
const { SecretId, SecretKey, Bucket, Region } = require("./cosConfig");

const cos = new COS({
  SecretId,
  SecretKey,
});

// cos存储桶中的路径地址
const path = ``;

// 上传文件到腾讯云cos
async function uploadImage(fileBuffer, type, name) {
  try {
    const data = await cos.putObject({
      Bucket,
      Region,
      Key: path + new Date().getTime() + name + `.${type}`,
      Body: fileBuffer,
      onProgress: (progressData) => {
        console.log(progressData);
      },
    });
    const imageUrl = `https://${data.Location}`;
    return imageUrl;
  } catch (error) {
    console.log(error);
  }
}

module.exports = {
  uploadImage,
};
